package com.fenghongzhang.day005;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * 1,布局
 * 2,在fragment 定义一个接口
 * 3,声明一个接口类型的成员
 * 4,点击事件后,调用接口中的方法.

 * 4.5 给myListener初始化
 * 5,让Activity这个接口.重写接口中的方法
 *
 * 6,在Activity中给textVIE赋值即可.
 */
public class InputFragment extends Fragment {
    private EditText fInput;
    private Button fBtn;
    public InputFragment() {
        // Required empty public constructor
    }


    //Activity是系统自动创建的.
    //3
    private MyListener myListener ;

    //2
    public interface MyListener{
        void message(String s);
    }

    //4.5
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        myListener = (MyListener) getActivity();
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_input, container, false);
        fInput = (EditText) inflate.findViewById(R.id.f_input);
        fBtn = (Button) inflate.findViewById(R.id.f_btn);

        //传值
        fBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = fInput.getText().toString();
                //4
                myListener.message(s);
            }
        });

        return inflate;
    }
}
